package com.skywolf.chem.manager;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.home.entity.TagsVo;
import com.skywolf.chem.manager.param.TagManagerParam;
import com.skywolf.chem.tags.entity.Tags;
import com.skywolf.chem.tags.service.ITagsService;
import com.skywolf.chem.usercenter.entity.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author 
 * @since 2021-01-21
 */
@RestController
@RequestMapping("/manager/tags")
public class TagsManagerController {

    @Autowired
    ITagsService tagsService;

    @ApiOperation(value = "获取标签")
    @RequestMapping(value = "/getTags",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<PageVO<TagsVo>> getTags(@RequestBody @Validated TagManagerParam queryParam) {
        PageVO<TagsVo> obj= tagsService.getTagsPage(queryParam);
        return RespData.success(obj);
    }

    @ApiOperation(value = "获取标签详情")
    @RequestMapping(value = "/getTagsById",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<Tags> getTagsById(@RequestParam(value = "id") Integer id) {
        Tags tag= tagsService.getById(id);
        return RespData.success(tag);
    }

    @ApiOperation(value = "创建标签")
    @RequestMapping(value = "/addTag",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> addTag(@RequestBody @Validated TagsVo tag) {
        QueryWrapper<Tags> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tname", tag.getTname());
        Tags tags = tagsService.getOne(queryWrapper);
        if(tags!=null){
            return RespData.failResult("该标签已经存在！");
        }
        tags = new Tags();
        tags.setTname(tag.getTname());
        tagsService.save(tags);
        return RespData.success("success");
    }

    @ApiOperation(value = "更新标签")
    @RequestMapping(value = "/updateTag",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> updateTag(@RequestBody @Validated TagsVo tag) {
        Tags tags = new Tags();
        tags.setTname(tag.getTname());
        tags.setId(tag.getId());
        tagsService.updateById(tags);
        return RespData.success("success");
    }

    @ApiOperation(value = "删除标签",notes = "删除标签")
    @RequestMapping(value = "/delTag",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delTag(@RequestParam(value = "id") Integer id) {
        tagsService.removeById(id);
        return RespData.success("success");
    }

}
